--[[	 FlupBuffBar
	written by Flup@EU-Rexxar
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

local Frame = CreateFrame("Frame")
Frame:RegisterEvent("PLAYER_AURAS_CHANGED")
local HavingBuffs = {}
local FreeTemp = {}
local TakenPos = {}
local Debuffs = {}
local BuffsChanged= 0

function FbbCreate()
local FbbMainFrame = CreateFrame("Frame", "FbbMainFrame", UIParent)
FbbMainFrame:EnableMouse(true)
if FbbDB.Config.Moveable == true then
FbbMainFrame:SetMovable(true)
FbbMainFrame:SetUserPlaced(true)
FbbMainFrame:RegisterForDrag("LeftButton")
FbbMainFrame:SetScript("OnDragStart", function() this:StartMoving() end)
FbbMainFrame:SetScript("OnDragStop", function() this:StopMovingOrSizing() end)
end
FbbMainFrame:SetWidth(FbbDB.Config.FrameWidth)
FbbMainFrame:SetHeight(FbbDB.Config.FrameHeight)
FbbMainFrame:SetBackdrop(FbbDB.Config.MainBackdrop)
FbbMainFrame:ClearAllPoints()
FbbMainFrame:SetScale(FbbDB.Config.Scale)
FbbMainFrame:SetPoint(unpack(FbbDB.Config.MainPoint))
FbbMainFrame:SetBackdropColor(unpack(FbbDB.Config.MainBackdropColor))

for t = 1, 16 do
	local FrameName = "FbbBuffFrame"..t
	local FrameName2 = "FbbIconFrame"..t
	local FrameName = CreateFrame("Button", FrameName, FbbMainFrame, "BuffButtonTemplate")
	FrameName:SetWidth(FbbDB.Config.BuffFrameWidth)
	FrameName:SetHeight(FbbDB.Config.BuffFrameHeight)
	FrameName.SetAlpha = function() end
	FrameName:SetBackdrop(FbbDB.Config.BuffBackdrop)
	FrameName:ClearAllPoints()
	FrameName:Hide()
	
	local FrameName2 = CreateFrame("Frame", FrameName2, FrameName)
	FrameName2:SetWidth(FbbDB.Config.BuffFrameHeight)
	FrameName2:SetHeight(FbbDB.Config.BuffFrameHeight)
	FrameName2.SetAlpha = function() end
	FrameName2:ClearAllPoints()
	FrameName2:SetPoint("TOPLEFT", FrameName,-FbbDB.Config.IconWidth,0)
	FrameName2:SetWidth(FbbDB.Config.IconWidth)
	FrameName2:SetHeight(FbbDB.Config.IconHeight)
	
	local FontName = "FbbFont"..t
	local FontName2 = "FbbTime"..t
	
	local FontName = FrameName:CreateFontString(FontName ,"OVERLAY")
	FontName:SetFont(FbbDB.Config.Font, FbbDB.Config.Fontsize)
	FontName:SetHeight(FbbDB.Config.BuffFrameHeight)
	FontName:SetPoint("LEFT",FrameName,FbbDB.Config.FontOffsetX1,FbbDB.Config.FontOffsetY1)
	FontName:SetVertexColor(unpack(FbbDB.Config.FontColor))
	
	local FontName2 = FrameName:CreateFontString(FontName2 ,"OVERLAY")
	FontName2:SetFont(FbbDB.Config.Font, FbbDB.Config.Fontsize)
	FontName2:SetHeight(FbbDB.Config.BuffFrameHeight)
	FontName2:SetPoint("LEFT",FrameName,FbbDB.Config.FontOffsetX2,FbbDB.Config.FontOffsetY2)
	FontName2:SetVertexColor(unpack(FbbDB.Config.FontColor))
end
end



Frame.onUpdate = function()
for u , t in pairs(Debuffs) do
Debuffs[u] = nil
end
local BuffHeight
if BuffsChanged == 0 then
for o = 1, 16 do
	local FrameNameH = getglobal("FbbBuffFrame"..o)
		FrameNameH:Hide()
	end
end

for r, l in pairs(TakenPos) do
TakenPos[r] = nil
end
for k, v in ipairs(HavingBuffs) do
	local Debuff = 0
	local HavingBuffTimeLeft = GetPlayerBuffTimeLeft(k)
	local TotalBuffTime = FbbDB.KnownBuffs[v]
	if TotalBuffTime == nil then 
		BuffHeight = -FbbDB.Config.BuffFrameHeight
	else
		local BuffTimePerCent = HavingBuffTimeLeft / TotalBuffTime
		BuffHeight = math.floor(FbbDB.Config.FrameHeight-(FbbDB.Config.FrameHeight*BuffTimePerCent)+0.5-50)
		BuffHeight = math.floor(((BuffHeight)*FbbDB.Config.Lambda)+0.5)	
	end
	
	HavingBuffTimeLeft = HavingBuffTimeLeft/60
	HavingBuffTimeLeft = string.format("%.2f", HavingBuffTimeLeft)
	BuffMinutesLeft, BuffSecondsLeft = strsplit(".", HavingBuffTimeLeft)
	BuffSecondsLeft =  BuffSecondsLeft/100*60
	BuffSecondsLeft  = math.floor(BuffSecondsLeft  + 0.5)
	if string.len(BuffSecondsLeft) == 1 then BuffSecondsLeft = "0"..BuffSecondsLeft end
	HavingBuffTimeLeft = BuffMinutesLeft..":"..BuffSecondsLeft
	
	local FrameNameD = getglobal("FbbBuffFrame"..k)
	local FrameName2D = getglobal("FbbIconFrame"..k)
	local FontName1D = getglobal("FbbFont"..k)
	local FontName2D = getglobal("FbbTime"..k)
		
	if BuffsChanged == 0 then
		FrameNameD:Show()
		FrameNameD:SetID(k)
		FrameNameD:SetScript("OnMouseDown", function() CancelPlayerBuff(k) end )
		FrameName2D:SetBackdrop({bgFile = GetPlayerBuffTexture(k)})
		child1 = FrameName2D:GetRegions()
		child1:SetTexCoord(0.1, 0.9, 0.1, 0.9)
		child1:ClearAllPoints()
		child1:SetPoint("TOPLEFT",FrameName2D, 1, -1)
		child1:SetPoint("BOTTOMRIGHT",FrameName2D, -1, 1)
		
		FontName1D:SetText(v)
		local TimeHide = getglobal("FbbBuffFrame"..k.."Duration")
		TimeHide:Hide()
		TimeHide.Show = function() end
		local Debuff = GetPlayerBuff(k, "HARMFUL")
		if Debuff then Debuffs[Debuff] = 1 end
		if Debuffs[k] == 1 then
			FrameNameD:SetBackdropColor(unpack(FbbDB.Config.DebuffColor))
		else 
			FrameNameD:SetBackdropColor(unpack(FbbDB.Config.BuffColor))
		end
	end
	BuffHeight = FindFreePos(BuffHeight)
	FrameNameD:SetPoint("TOPLEFT", FbbMainFrame,FbbDB.Config.BuffFrameXoffset,-BuffHeight)
	if HavingBuffTimeLeft == "0:00" then
		FontName2D:SetText("")
	else
		FontName2D:SetText(HavingBuffTimeLeft)
	end
end
	BuffsChanged = 1
end

Frame.onEvent = function(event, ...)
HavingBuffs = {}
BuffsChanged = 0
for i = 1, 16 do
	local BuffName = GetPlayerBuffName(i)
	local BuffTimeLeft =  math.floor(GetPlayerBuffTimeLeft(i) +0.5)
	if BuffName then
		if FbbDB.KnownBuffs[BuffName] == nil and BuffTimeLeft > 0 then
			FbbDB.KnownBuffs[BuffName] = BuffTimeLeft
		elseif  FbbDB.KnownBuffs[BuffName] ~= nil and FbbDB.KnownBuffs[BuffName] < BuffTimeLeft then
			FbbDB.KnownBuffs[BuffName] = BuffTimeLeft
		end
		table.insert(HavingBuffs, BuffName)
	end
end
end


function FindFreePos(pos)
local FreeCount = 0
for r, l in pairs(FreeTemp) do
	FreeTemp[r] = nil
end
repeat
	if TakenPos[pos] == nil then
		FreeCount = 1 + FreeCount
		FreeTemp[pos] = 1
	else
		FreeCount = 0
		for r, l in pairs(FreeTemp) do
			FreeTemp[r] = nil
		end
	end
pos = pos + 1
until FreeCount == FbbDB.Config.BuffFrameHeight

for w,e in pairs (FreeTemp) do
	TakenPos[w] = e
end
return pos
end


Frame:SetScript("OnUpdate",Frame.onUpdate)
Frame:SetScript("OnEvent",Frame.onEvent)